﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.UI;
using Silvermoon.Images;
using Silvermoon.Windows;
using ShoppingList.Biz;
using ShoppingList.DTO;
using Silvermoon.Controls.Forms;

namespace ShoppingList.Pages
{
    public class NewProductPage : Window
    {
        private TextBox textBox;
        private ToolbarButton okButton;
        private Product product;

        public NewProductPage(Product product)
            : base()
        {
            if (product == null) throw new ArgumentNullException("product");

            this.Product = product;
            InitializePage();
        }

        public Product Product
        {
            get { return product; }
            private set { product = value; }
        }

        private void InitializePage()
        {
            Styles = StylePool.Main;
            Title = "NEW PRODUCT";
            SetTransition(300, TransitionMask.Fade | TransitionMask.Top, TransitionMask.Fade | TransitionMask.Explode);
            Color = PhoneColors.AlphaColor(PhoneColors.Dark, 240);

            ScrollStackPanel sp = new ScrollStackPanel { Margin = new Margin(10, 10, 10, 0) };
            Controls.Add(sp);

            Label l = new Label { Text = "Product name", AutoSize = true, Font = FontPool.MediumRegularFont, FontScale = 1.5f };
            sp.Controls.Add(l);

            textBox = new TextBox { Margin = new Margin(0, 10, 0, null), AutoHeight = true, Font = FontPool.MediumRegularFont, Text = Product.Name };
            textBox.TextChanged += new EventHandler(OnTextChanged);
            sp.Controls.Add(textBox);

            Toolbar tb = new Toolbar();
            okButton = tb.AddButton(SysImages.Ok);
            okButton.Enabled = false;
            okButton.Tap += new EventHandler<TapEventArgs>(OnOk);
            tb.AddButton(SysImages.RecycleBin).Tap += new EventHandler<TapEventArgs>(OnDelete);
            tb.AddButton(SysImages.Cancel).Tap += new EventHandler<TapEventArgs>(NewProductPage_Tap);

            tb.Buttons[1].Enabled = Product.Id != 0;
            Toolbar = tb;
        }

        void OnDelete(object sender, TapEventArgs e)
        {
            MessageDialog.Show("Delete Product?", textBox.Text + " will be deleted from your list of products.", "Delete", "Cancel", DeleteProduct, null);
        }

        private void DeleteProduct()
        {
            if (Delete != null) Delete(this, EventArgs.Empty);
            Hide();
        }

        void OnOk(object sender, TapEventArgs e)
        {
            Product product = Product;
            if (product != null)
            {
                product.Name = textBox.Text;
                if (BizContext.Context.SaveProduct(product))
                {
                    if (Ok != null) Ok(this, EventArgs.Empty);
                    Hide();
                }
            }
        }

        void OnTextChanged(object sender, EventArgs e)
        {
            string text = textBox.Text;
            okButton.Enabled = !string.IsNullOrEmpty(text) && !ProductExists(text);
        }

        private bool ProductExists(string text)
        {
            var context = BizContext.Context;
            return context.Products.Any(s => string.Compare(text, s.Name, true) == 0);

        }

        void NewProductPage_Tap(object sender, TapEventArgs e)
        {
            Hide();
        }

        protected override void OnHidden()
        {
            base.OnHidden();
            Dispose();
        }

        protected override void OnShown()
        {
            base.OnShown();
            textBox.SelectAll();
            textBox.SetFocus();
        }

        public event EventHandler Ok;
        public event EventHandler Delete;
    }
}
